package com.stylefeng.guns.modular.system.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.stylefeng.guns.common.exception.BizExceptionEnum;
import com.stylefeng.guns.common.exception.BussinessException;
import com.stylefeng.guns.common.persistence.service.impl.PointServiceImpl;
import com.stylefeng.guns.core.util.DateUtil;
import com.stylefeng.guns.modular.system.dao.DictDao;
import com.stylefeng.guns.modular.system.service.IDictService;
import com.stylefeng.guns.common.persistence.dao.DictMapper;
import com.stylefeng.guns.common.persistence.model.Dict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

import static com.stylefeng.guns.common.constant.factory.MutiStrFactory.*;

@Service
@Transactional
public class DictServiceImpl extends PointServiceImpl<DictMapper, Dict> implements IDictService{

    @Resource
    DictDao dictDao;

    @Resource
    DictMapper dictMapper;
    @Override
    public void addDict(String dictName, String dictValues, String modular) {
        //判断有没有该字典
        List<Dict> dicts = dictMapper.selectList(new EntityWrapper<Dict>().eq("name", dictName).and().eq("pid", 0));
        if(dicts != null && dicts.size() > 0){
            throw new BussinessException(BizExceptionEnum.DICT_EXISTED);
        }

        //解析dictValues
        List<Map<String, String>> items = parseKeyValue(dictValues);

        //添加字典
        Dict dict = new Dict();
        dict.setName(dictName);
        dict.setNum(0);
        dict.setPid("0");
        dict.setModular(modular);
        this.dictMapper.insert(dict);

        //添加字典条目
        for (Map<String, String> item : items) {
            String num = item.get(MUTI_STR_KEY);
            String name = item.get(MUTI_STR_VALUE);
            Dict itemDict = new Dict();
            itemDict.setPid(dict.getId());
            itemDict.setName(name);
            try {
                itemDict.setNum(Integer.valueOf(num));
            }catch (NumberFormatException e){
                throw new BussinessException(BizExceptionEnum.DICT_MUST_BE_NUMBER);
            }
            this.dictMapper.insert(itemDict);
        }
    }
    @Override
    public void editDict(String dictId, String dictName, String dicts, String modular) {
        //删除之前的字典
        this.delteDict(dictId);

        //重新添加新的字典
        this.addDict(dictName,dicts,modular);
    }
    @Override
    public void delteDict(String dictId) {
        //删除这个字典的子词典
        Wrapper<Dict> dictEntityWrapper = new EntityWrapper<>();
        dictEntityWrapper = dictEntityWrapper.eq("pid", dictId);
        dictMapper.delete(dictEntityWrapper);

        //删除这个词典
        dictMapper.deleteById(dictId);
    }

    @Override
    public List<Dict> getByPName(String pname) {

        Dict dictByName = selectByName(pname);
        List<Dict> dicts = dictMapper.selectListByPid(dictByName.getId().toString());
        return dicts;
    }

    @Override
    public List<Dict> getByPNameNotTop(String name) {
        Dict dictByName = dictMapper.selectByNameAndPid(name);
        List<Dict> dicts;
        if(dictByName == null){
            dicts = new ArrayList<>();
        }else{
            dicts = dictMapper.selectListByPid(dictByName.getId().toString());
        }

        return dicts;
    }


    @Override
    public List<Dict> getByPCode(String pcode) {
        List<Dict> dicts = dictMapper.selectByPcode(pcode);
        return dicts;
    }

    @Override
    public List<Dict> getYears() {
        List<Dict> years = getByPCode("year");
        String nowYear = DateUtil.getYear();
        Dict y = null;

        for (Dict year : years) {
            if(year.getNum().toString().equals(nowYear)){
                y = year;
            }
        }
        years.add(0,y);
        return years;
    }

    @Override
    public List<Dict> getByPId(String pid) {
        return dictMapper.selectListByPid(pid);
    }

    @Override
    public Dict selectByName(String name) {
        return dictMapper.selectByName(name);
    }

    @Override
    public List<Dict> getChildByMap(Map<String, Object> condition) {
        List<Dict> dicts = super.selectByMap(condition);
        if(dicts != null && dicts.size() > 0){
            Dict dict = dicts.get(0);

            return this.getByPId(dict.getId().toString());
        }else{
            return new ArrayList<>();
        }
    }

    @Override
    public List<Dict> getChildByCode(String code) {
        Map<String,Object> condition = new HashMap<>();
        condition.put("code", code);
        List<Dict> dicts = this.selectByMap(condition);
        if(dicts != null || dicts.size() > 1){
            Dict dict = dicts.get(0);
            List<Dict> byPId = this.getByPId(dict.getId());
            return byPId;
        }else{
            List<Dict> byPId = new ArrayList<>();
            return byPId;
        }

    }

    @Override
    public Map<String, String> getMapNameNumByPcode(String pcode) {
        Dict dict = dictMapper.selectByCode(pcode);
        List<Dict> dicts = this.getByPId(dict.getId());
        Map<String,String> map = new HashMap<>();
        for (Dict d : dicts) {
            map.put(d.getName(),d.getNum().toString());
        }
        return map;
    }
    @Override
    public Map<String, String> getMapNumNameByPcode(String pcode) {
        Dict dict = dictMapper.selectByCode(pcode);
        List<Dict> dicts = this.getByPId(dict.getId());
        Map<String,String> map = new HashMap<>();
        for (Dict d : dicts) {
            map.put(d.getNum().toString(),d.getName());
        }
        return map;
    }

    @Override
    public Map<String, String> getMapNameCodeByPcode(String pcode) {
        Dict dict = dictMapper.selectByCode(pcode);
        List<Dict> dicts = this.getByPId(dict.getId());
        Map<String,String> map = new HashMap<>();
        for (Dict d : dicts) {
            map.put(d.getName(),d.getCode());
        }
        return map;
    }


}
